public class Rational {
   int numerator = 1 ;   //·Ö×Ó
   int denominator = 1; //·ÖÄ¸
   void setNumerator(int a) {  //ÉèÖÃ·Ö×Ó
      int c=f(Math.abs(a),denominator);  //¼ÆËã×î´ó¹«Ô¼Êý
      numerator = a/c; 
      denominator = denominator/c;
      if(numerator<0&&denominator<0) {
          numerator = -numerator;
          denominator = -denominator;
      }
   }
   void setDenominator(int b) {  //ÉèÖÃ·ÖÄ¸
      int c=f(numerator,Math.abs(b));  //¼ÆËã×î´ó¹«Ô¼Êý
      numerator = numerator/c;
      denominator = b/c;
      if(numerator<0&&denominator<0) {
          numerator = -numerator;
          denominator = -denominator;
      }
   }
   int getNumerator() {
      return numerator;
   }
   int getDenominator() {
     return denominator;
   }  
   int f(int a,int b) { //ÇóaºÍbµÄ×î´ó¹«Ô¼Êý
      if(a==0) return 1;
      if(a<b) {
         int c=a;
         a=b;
         b=c; 
      }
      int r=a%b;
      while(r!=0) {
         a=b;
         b=r;
         r=a%b;
      } 
      return b;
   }
   Rational add(Rational r) {  //¼Ó·¨ÔËËã
      int a=r.getNumerator();
      int b=r.getDenominator();
      int newNumerator=numerator*b+denominator*a; //¼ÆËã³öÐÂ·Ö×Ó
      int newDenominator=denominator*b;           //¼ÆËã³öÐÂ·ÖÄ¸
      Rational result=new Rational(); 
      result.setNumerator(newNumerator);
      result.setDenominator(newDenominator);
      return result;
   }
   Rational sub(Rational r) {  //¼õ·¨ÔËËã
      int a=r.getNumerator();
      int b=r.getDenominator();
      int newNumerator=numerator*b-denominator*a;
      int newDenominator=denominator*b;
      Rational result=new Rational(); 
      result.setNumerator(newNumerator);
      result.setDenominator(newDenominator);
      return result;
   }
   Rational muti(Rational r) { //³Ë·¨ÔËËã
      int a=r.getNumerator();
      int b=r.getDenominator();
      int newNumerator=numerator*a;
      int newDenominator=denominator*b;
      Rational result=new Rational(); 
      result.setNumerator(newNumerator);
      result.setDenominator(newDenominator);
      return result;
   }
   Rational div(Rational r)  { //³ý·¨ÔËËã
      int a=r.getNumerator();
      int b=r.getDenominator();
      int newNumerator=numerator*b;
      int newDenominator=denominator*a;
      Rational result=new Rational(); 
      result.setNumerator(newNumerator);
      result.setDenominator(newDenominator);
      return result;
   }
}



